package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-02
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterVideoById(String courseId) {

        //1.根据课程ID查询课程里面所有章节
        QueryWrapper<EduChapter> eduChapter = new QueryWrapper<>();
        eduChapter.eq("course_id", courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(eduChapter);

        //2.根据课程ID查询课程里面所有小结
        QueryWrapper<EduVideo> eduVideo = new QueryWrapper<>();
        eduVideo.eq("course_id", courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(eduVideo);

        //创建list集合，用于封装最后的数据
        List<ChapterVo> finalList = new ArrayList<>();
        //3.遍历查询章节list，进行封装
        for (int i = 0; i < eduChapterList.size(); i++) {
            EduChapter eduChapter1 = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter1, chapterVo);
            finalList.add(chapterVo);


            //4.遍历查询小结list，进行封装
            List<VideoVo> videoVoList = new ArrayList<>();
            for (int j = 0; j < eduVideoList.size(); j++) {
                EduVideo eduVideo1 = eduVideoList.get(j);
                if (eduVideo1.getChapterId().equals(eduChapter1.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo1, videoVo);
                    videoVoList.add(videoVo);
                }

            }
            chapterVo.setChildren(videoVoList);
        }


        return finalList;
    }

    @Override
    public Boolean deleteChapterById(String chapterId) {

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = eduVideoService.count(wrapper);
        if (count > 0) {//有小节数据，不能删
            throw new GuliException(20001, "不能删除");
        } else {
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }

    }

    @Override
    public void removeChapterById(String courseId) {
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
